﻿Imports Telerik.Windows.Controls
Imports Telerik.Windows.Data
Imports CRM.BusinessLayer
Imports System.Data
Imports System.Text
Partial Public Class frmAdminCounties
    Private intSaveNeworEdit As Integer
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmAdminCounties_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        refreshList()
        Me.txtCounty.IsEnabled = False
        Me.btnSave.IsEnabled = False
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = False
        Me.btnDelete.IsEnabled = False
    End Sub
    Private Sub refreshList()
        Try
            Me.grdCounties.ItemsSource = Admin.CountyList
            Me.grdCounties.Columns(0).IsVisible = False
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminCounties-refreshList")
        End Try
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddNew.Click
        intSaveNeworEdit = 1
        ClearControls()
        Me.txtCounty.IsEnabled = True
        Me.btnSave.IsEnabled = True
        Me.btnAddNew.IsEnabled = False
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = True
        Me.btnDelete.IsEnabled = False
        Me.txtCounty.Focus()
    End Sub

    Private Sub ClearControls()
        Me.txtCounty.Text = String.Empty
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        intSaveNeworEdit = 0
        ClearControls()
        Me.txtCounty.IsEnabled = False
        Me.btnAddNew.IsEnabled = True
        Me.btnSave.IsEnabled = False
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = False
        Me.btnDelete.IsEnabled = False
        Me.grdCounties.IsEnabled = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEdit.Click
        intSaveNeworEdit = 2
        ClearControls()
        Me.txtCounty.IsEnabled = True
        Me.btnSave.IsEnabled = True
        Me.btnCancel.IsEnabled = True
        Me.btnDelete.IsEnabled = False
    End Sub

    Private Sub grdCounties_SelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs) Handles grdCounties.SelectionChanged

        If Me.grdCounties.SelectedItems.Count = 0 Then
            Me.grdCounties.IsEnabled = False
            Me.btnEdit.IsEnabled = False
            Me.btnCancel.IsEnabled = False
            Me.btnDelete.IsEnabled = False
            Exit Sub
        Else
            'Me.grdCounties.IsEnabled = False
            Me.btnEdit.IsEnabled = True
            Me.btnCancel.IsEnabled = True
            Me.btnDelete.IsEnabled = True
            Dim selectedRow As DataRow = grdCounties.SelectedItem
            If Not IsNothing(selectedRow) Then
                txtCounty.Text = selectedRow(1).ToString()
            End If
        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        If txtCounty.Text.Trim = String.Empty Then
            CommonFuncs.DisplayMessage("Please enter County !", CommonDialog.CustomDialogIcons.Information, "Error")
            txtCounty.Focus()
            Exit Sub
        Else
            btnSave.IsEnabled = False

            'grdConsult.Enabled = True
            Dim sql As StringBuilder
            Select Case intSaveNeworEdit
                Case 1 'new one
                    sql = New StringBuilder
                    With sql
                        .Append("")
                        .Append("insert into l_county ")
                        .Append("(County_Name)")
                        .Append(" Values(")
                        .Append(" '" & Me.txtCounty.Text & "'")
                        .Append(")")
                    End With
                    Try
                        If CommonDataFunctions.ExecuteSQL(sql.ToString) = True Then
                            refreshList()
                            CommonFuncs.DisplayMessage("The new County has been added successfully.", CommonDialog.CustomDialogIcons.Information, "Success")

                            'Exit Sub
                        Else
                            CommonFuncs.DisplayMessage("There is an error adding the County to the system. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                            'Exit Sub
                        End If
                    Catch ex As Exception
                        CommonFuncs.ErrorHandler(ex, "frmAdminCounties-btnSave_Click")
                        Exit Sub
                    End Try
                   
                Case 2 'existing one
                    sql = New StringBuilder
                    With sql
                        .Append("update l_county set ")
                        .Append(" County_Name = '" & Me.txtCounty.Text & "'")
                        Dim selectedRow As DataRow = grdCounties.SelectedItem
                        .Append(" where County_ID = " & selectedRow(0))
                    End With
                    Try
                        If CommonDataFunctions.ExecuteSQL(sql.ToString) = True Then
                            refreshList()
                            CommonFuncs.DisplayMessage("The County's details have been updated successfully.", CommonDialog.CustomDialogIcons.Information, "Success")
                            'Exit Sub
                        Else
                            CommonFuncs.DisplayMessage("There is an error in editing the County's details. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                            'Exit Sub
                        End If
                    Catch ex As Exception
                        CommonFuncs.ErrorHandler(ex, "frmAdminCounties-btnSave_Click")
                        Exit Sub
                    End Try


            End Select
            ClearControls()
            txtCounty.IsEnabled = False
            grdCounties.IsEnabled = True
            btnCancel.IsEnabled = False
            btnAddNew.IsEnabled = True
            btnEdit.IsEnabled = False
            btnCancel.IsEnabled = False
        End If

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click
        Dim deleteOption As MsgBoxResult
        deleteOption = MsgBox("Are you really sure to delete County ?", MsgBoxStyle.OkCancel)
        If deleteOption = MsgBoxResult.Ok Then
            Dim selectedRow As DataRow = grdCounties.SelectedItem
            Dim Sql As New StringBuilder
            With Sql
                .Append("delete  from l_county where County_ID=" & selectedRow(0).ToString())
            End With

            If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                btnDelete.IsEnabled = False
                btnCancel.IsEnabled = False
                btnEdit.IsEnabled = False
                btnSave.IsEnabled = False
                grdCounties.IsEnabled = True
                ClearControls()
                refreshList()
                CommonFuncs.DisplayMessage("The County Details has been deleted successfully.", CommonDialog.CustomDialogIcons.Information, "Success")
            Else
                CommonFuncs.DisplayMessage("There is an error while deleting the County to the system. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                'Exit Sub
            End If

        ElseIf deleteOption = MsgBoxResult.Cancel Then

        End If

    End Sub
End Class
